package cn.leedsoft.and.crimeintent.controller.crime_intent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import cn.leedsoft.and.crimeintent.R;
import cn.leedsoft.and.crimeintent.model.Crime;
import cn.leedsoft.and.crimeintent.model.CrimeLab;

import java.util.List;
import java.util.UUID;

/**
 * Created by leeds on 2016/8/8 20:44.
 */
public class DetailPagerActivity extends FragmentActivity{
    private ViewPager mViewPager;
    private List<Crime> mCrimes;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.detail_view_pager);
        setContentView(mViewPager);

        mCrimes = CrimeLab.getInstance(this).getCrimes();

        FragmentManager fragmentManager = getSupportFragmentManager();
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                Crime crime = mCrimes.get(position);
                return CrimeDetailFragment.newInstance(crime.getId());
            }

            @Override
            public int getCount() {
                return mCrimes.size();
            }
        });

        UUID uuid = (UUID) getIntent().getSerializableExtra(CrimeDetailFragment.INTENT_KEY_CRIME_ID);
        for (int i=0; i<mCrimes.size(); i++) {
            if(mCrimes.get(i).getId().equals(uuid)) {
                mViewPager.setCurrentItem(i);
            }
        }

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Crime crime = mCrimes.get(position);
                setTitle(crime.getTitle());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
